var {
  clickUtils,
  randomUtils,
  textUtils,
  ocrUtils,
  logUtils,
} = require("../../utils/index");

var adBaiDu = require("../../ads/baiDu/index");

var Action = require("../../base/Action");
var common = require("./common");
var pointMap = require("./pointMap");
var aInvite = new Action("邀请好友", {});
aInvite.goStartPage = function () {
  if (common.inHome()) {
    clickUtils.clickPointMap(pointMap.首页.我的);
    sleep(randomUtils.getRandom(1.5, 2, 1000));
    if (randomUtils.possible(90, 100)) {
      clickUtils.clickPointMap(pointMap.我的.设置);
      sleep(randomUtils.getRandom(2, 5, 1000));
      back();
      sleep(randomUtils.getRandom(2, 5, 1000));
    }
    var content = "邀请好友";
    var TextUI = textUtils.existsTextStartsWith(content);
    if (TextUI) {
      clickUtils.clickTextUI(TextUI);
      sleep(randomUtils.getRandom(2, 3.5, 1000));
      return true;
    }
  }
};
aInvite.exec = function () {
  var content = "明天再来";
  var TextOCR = ocrUtils.captureExistsText(content);
  if (TextOCR) {
    this.todayIsOver = true;
    return;
  }
  var content = "必得199金币";
  var TextUI = textUtils.existsTextEndsWith(content);
  if (TextUI) {
    this.share5();
  }
  this.openBless();
};
aInvite.openBless = function () {
  var content = "开福袋";
  var TextUI = textUtils.existsTextStartsWith(content);
  if (TextUI) {
    clickUtils.clickTextUI(TextUI);
    sleep(randomUtils.getRandom(2, 3.5, 1000));
    var contents = ["看广告再", "看广告得", "看视频再", "看广告领", "看视频领"];
    var TextUI = textUtils.existsOneTextStartsWith(contents);
    if (TextUI) {
      clickUtils.clickTextUI(TextUI);
      sleep(randomUtils.getRandom(2, 3.5, 1000));
      this.watchAd();
      return;
    }
    var TextOCR = ocrUtils.captureExistsOneText(contents);
    if (TextOCR) {
      clickUtils.clickTextOCR(TextOCR);
      sleep(randomUtils.getRandom(2, 3.5, 1000));
      this.watchAd();
      return;
    }
  }
};
aInvite.watchAd = function () {
  adBaiDu.startWatchAd(randomUtils.getRandom(2, 4));
  var contents = ["开心收下", "立即收下"];
  var TextUI = textUtils.existsOneTextStartsWith(contents);
  if (TextUI) {
    if (randomUtils.possible(0, 80)) {
      clickUtils.clickTextUI(TextUI);
      sleep(randomUtils.getRandom(2, 3.5, 1000));
    }
    return;
  }
  var TextOCR = ocrUtils.captureExistsOneText(contents);
  if (TextOCR) {
    if (randomUtils.possible(0, 80)) {
      clickUtils.clickTextOCR(TextOCR);
      sleep(randomUtils.getRandom(2, 3.5, 1000));
    }
    return;
  }
};
aInvite.share5 = function () {
  for (var index = 0; index < 5; index++) {
    var content = "必得199金币";
    var TextUI = textUtils.existsTextEndsWith(content);
    if (TextUI) {
      clickUtils.clickTextUI(TextUI);
      sleep(randomUtils.getRandom(2, 3.5, 1000));
      var content = "去粘贴";
      var TextUI = textUtils.existsTextStartsWith(content);
      if (TextUI) {
        clickUtils.clickTextUI(TextUI);
        sleep(randomUtils.getRandom(5, 7, 1000));
        back();
        sleep(randomUtils.getRandom(3, 5, 1000));
        var content = "继续分享";
        var TextUI = textUtils.existsTextStartsWith(content);
        if (TextUI) {
          clickUtils.clickTextUI(TextUI);
          sleep(randomUtils.getRandom(3, 5, 1000));
        } else {
          var content = "开心收下";
          var TextUI = textUtils.existsTextStartsWith(content);
          if (TextUI) {
            clickUtils.clickTextUI(TextUI);
            sleep(randomUtils.getRandom(3, 5, 1000));
            break;
          }
        }
      }
    } else {
      logUtils.info("没找到:", index, content);
    }
  }
};

module.exports = aInvite;
